package com.yang.SpringTest.annotation.scheduledLearn;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * <p>Tasks</p>
 *
 * @author By: chengxuyuanshitang <br>
 * @Package: com.yang.SpringTest.annotation.scheduledLearn <br>
 * @CreateTime: 2025-02-06 09:30 <br>
 */
@Component
public class ScheduledDemoTasks {

    /**
     * 每5秒执行一次
     */
    @Scheduled(fixedRate = 5000)
    public void task1() {
        System.out.println("****************Task 1 executed at: " + System.currentTimeMillis());
    }

    /**
     * 每天中午12点执行
     */
    @Scheduled(cron = "0 0 12 * * ?", zone = "GMT+8")
    public void task2() {
        System.out.println("****************Task 2 executed at: " + System.currentTimeMillis());
    }

    /**
     * 首次延迟10秒，之后每5秒执行一次
     */
    @Scheduled(initialDelay = 10, fixedRate = 5, timeUnit = TimeUnit.SECONDS)
    public void task3() {
        System.out.println("**************** Task 3 executed at: " + System.currentTimeMillis());
    }
}